@@ -0,0 +1,17 @@ |
||
1 |
+{ |
|
2 |
+ "response":{ |
|
3 |
+ "docs":[ |
|
4 |
+ { |
|
5 |
+ "url": "http://www.stubhub.com/event/name-1-1-2014-12345", |
|
6 |
+ "seo_description_en_US": "name", |
|
7 |
+ "event_date_local": "2014-01-01", |
|
8 |
+ "maxPrice": "100", |
|
9 |
+ "minPrice": "50", |
|
10 |
+ "totalPostings": "100", |
|
11 |
+ "totalTickets": "200", |
|
12 |
+ "venue_name": "Venue Name" |
|
13 |
+ } |
|
14 |
+ ] |
|
15 |
+ } |
|
16 |
+} |
|
17 |
+ |
@@ -0,0 +1,68 @@ |
||
1 |
+require 'spec_helper' |
|
2 |
+ |
|
3 |
+describe Agents::StubhubAgent do |
|
4 |
+ |
|
5 |
+ let(:name) { 'Agent Name' } |
|
6 |
+ let(:url) { 'http://www.stubhub.com/event/name-1-1-2014-12345' } |
|
7 |
+ let(:parsed_body) { JSON.parse(body)['response']['docs'][0] } |
|
8 |
+ let(:valid_params) { { 'url' => parsed_body['url'] } } |
|
9 |
+ let(:body) { File.read(Rails.root.join('spec/data_fixtures/stubhub_data.json')) } |
|
10 |
+ let(:stubhub_event_id) { 12345 } |
|
11 |
+ let(:response_payload) { { |
|
12 |
+ 'url' => url, |
|
13 |
+ 'name' => parsed_body['seo_description_en_US'], |
|
14 |
+ 'date' => parsed_body['event_date_local'], |
|
15 |
+ 'max_price' => parsed_body['maxPrice'], |
|
16 |
+ 'min_price' => parsed_body['minPrice'], |
|
17 |
+ 'total_postings' => parsed_body['totalPostings'], |
|
18 |
+ 'total_tickets' => parsed_body['totalTickets'], |
|
19 |
+ 'venue_name' => parsed_body['venue_name'] |
|
20 |
+ } } |
|
21 |
+ |
|
22 |
+ before do |
|
23 |
+ stub_request(:get, "http://www.stubhub.com/listingCatalog/select/?q=%2B%20stubhubDocumentType:event%0D%0A%2B%20event_id:#{stubhub_event_id}%0D%0A&rows=10&start=0&wt=json"). |
|
24 |
+ with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Host'=>'www.stubhub.com', 'User-Agent'=>'Ruby'}). |
|
25 |
+ to_return(:status => 200, :body => body, :headers => {}) |
|
26 |
+ |
|
27 |
+ @stubhub_agent = described_class.new(name: name, options: valid_params) |
|
28 |
+ @stubhub_agent.user = users(:jane) |
|
29 |
+ @stubhub_agent.save! |
|
30 |
+ end |
|
31 |
+ |
|
32 |
+ |
|
33 |
+ describe "#check" do |
|
34 |
+ |
|
35 |
+ it 'should create an event' do |
|
36 |
+ expect { @stubhub_agent.check }.to change { Event.count }.by(1) |
|
37 |
+ end |
|
38 |
+ |
|
39 |
+ it 'should properly parse the response' do |
|
40 |
+ event = @stubhub_agent.check |
|
41 |
+ event.payload.should == response_payload |
|
42 |
+ end |
|
43 |
+ end |
|
44 |
+ |
|
45 |
+ describe "validations" do |
|
46 |
+ before do |
|
47 |
+ @stubhub_agent.should be_valid |
|
48 |
+ end |
|
49 |
+ |
|
50 |
+ it "should require a url" do |
|
51 |
+ @stubhub_agent.options['url'] = nil |
|
52 |
+ @stubhub_agent.should_not be_valid |
|
53 |
+ end |
|
54 |
+ |
|
55 |
+ end |
|
56 |
+ |
|
57 |
+ describe "#working?" do |
|
58 |
+ it "checks if events have been received within the expected receive period" do |
|
59 |
+ @stubhub_agent.should_not be_working |
|
60 |
+ |
|
61 |
+ Agents::StubhubAgent.async_check @stubhub_agent.id |
|
62 |
+ @stubhub_agent.reload.should be_working |
|
63 |
+ two_days_from_now = 2.days.from_now |
|
64 |
+ stub(Time).now { two_days_from_now } |
|
65 |
+ @stubhub_agent.reload.should_not be_working |
|
66 |
+ end |
|
67 |
+ end |
|
68 |
+end |